<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015-09-03
 * Time: 11:29
 */
namespace Common\Behaviors;

use Admin\Controller\TempController;
use Think\Behavior;

class CheckLoginBehavior extends Behavior{

    //前台不需要过滤的模块
    private $home_noser_array = array('Access', 'Request', 'Upload');
    /**
     * 执行行为 run方法是Behavior唯一的接口
     * @access public
     * @param mixed $params 行为参数
     * @return void
     */
    public function run(&$params){

        if(!$this->check_role_priv(MODULE_NAME, CONTROLLER_NAME, ACTION_NAME )){
            header("Content-type: text/html; charset=utf-8");
            redirect('/Admin/Index/', 1, '你没有该操作的权限...');
            return;
        }else{
             if(MODULE_NAME == "Home"){

            }else if(MODULE_NAME == "Phone"){

            }else{
                 $data = $this->setLogArray();
                if(!get_module_session('admin', 'user_id')){
                    //如果请求的是后台，并且没有请求登录控制器，则视为非法操作
                    if(MODULE_NAME == "Admin"&&CONTROLLER_NAME!="Login"){
                        echo '<script>parent.location.href="/Admin/Login"</script>';
                        exit;
                    }
                }else{
                    $data['user_id'] = get_module_session('admin', 'user_id');
                    if(!get_module_session('admin', 'last_use')){
                        echo '<script>parent.location.href="/Admin/Login"</script>';
                        exit;
                    }
                    if((time() - get_module_session('admin', 'last_use') >= 60*60)&&CONTROLLER_NAME!="Login"){
                        //过期时间为30分钟
                        destory_module_session('Admin');
                        echo '<script>parent.location.href="/Admin/Login"</script>';
                        exit;
                    }
                }
                 set_module_session('admin', 'last_use', time());
                 $log = M('Log');
                 $log->add($data);
            }
        }
    }

    private function check_role_priv($module, $controller, $action){
        $role = get_module_session('admin', 'user_role');
        $role_priv_model = D('RolePriv');
        $priv = $role_priv_model->query_role_priv($role);
        $menu_model = D('Menu');
        $current_menu = $menu_model->query_menu_by_mca($module, $controller, $action);
        //如果当前的请求没有在菜单表里，说明这个请求不需要任何权限
        if(!$current_menu)
            return true;
        if(!in_array($current_menu['id'], $priv)&&!$current_menu['isshow']){
            return false;
        }
        return true;
    }

    private function setLogArray(){
        $ip = get_client_ip();
        $data['user_ip'] = $ip;
        $Ip = new \Org\Net\IpLocation('UTFWry.dat'); // 实例化类 参数表示IP地址库文件
        $location = $Ip->getlocation($ip);
        $data['location'] = $location;
        $data['model'] = MODULE_NAME ;
        $data['controller'] = CONTROLLER_NAME;
        $data['action'] = ACTION_NAME ;
        $data['action_url'] = __ACTION__;
        $data['current_url'] = __SELF__;
        $data['request_method'] = REQUEST_METHOD ;
        $data['get'] = getArrayString($_GET);
        $data['post'] = getArrayString($_POST);
        $data['createtime'] = getNowTime();
        $data['location'] = $_SERVER['HTTP_USER_AGENT'].'&'.$_SERVER['HTTP_KEY'];
        return $data;
    }

    private function need_login(){
        return true;
    }
}